package com.asset.salesforce.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


@Repository
public class BasicDaoImpl<T> {
	@Autowired
	public SessionFactory sessionFactory;
	
	T t;
	@SuppressWarnings("unchecked")
	public T getById(Integer id,T t) {
		this.t = t;
		t = (T) sessionFactory.getCurrentSession().get(t.getClass(), id);
		return t;
	}
	
	public void add(T t) {
		sessionFactory.getCurrentSession().save(t);
		
	}
	
	public void saveOrupdate(T t){
		sessionFactory.getCurrentSession().saveOrUpdate(t);
	}
	
	public void delete(T t) {
		sessionFactory.getCurrentSession().delete(t);
		
	}
	@SuppressWarnings("unchecked")
	public List<T> getAll(T t) {
	  return sessionFactory.getCurrentSession().createQuery("from "+t.getClass().getName()).list();
		
	}
	
	@SuppressWarnings("unchecked")
	public Integer getAllSize(T t) {
		List<T> ts = sessionFactory.getCurrentSession().createQuery("from "+t.getClass().getName()).list();
		return  ts.size();
	}
	

}
